package com.yunji.cabinet.service.impl;

import com.yunji.base.bo.ChangeBindBO;
import com.yunji.base.cache.BaseCache;
import com.yunji.cabinet.bo.*;
import com.yunji.cabinet.context.convert.CabinetConvert;
import com.yunji.cabinet.dao.ICabinetCellDao;
import com.yunji.cabinet.dao.ICabinetDao;
import com.yunji.cabinet.dao.ICabinetSiteDao;
import com.yunji.cabinet.dao.IContainerScanDao;
import com.yunji.cabinet.domain.ContainerDO;
import com.yunji.cabinet.domain.CabinetSiteDO;
import com.yunji.cabinet.domain.ContainerScanDO;
import com.yunji.cabinet.params.SpecificUpgradeCabinetParam;
import com.yunji.cabinet.service.ICabinetService;
import com.yunji.cabinet.vo.SpecificUpgradeCabinetVO;
import com.yunji.common.model.cache.CabinetCacheDTO;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.dto.StorageFiledDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.cabinet.service.impl
 * @FileName CabinetServiceImpl.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-04-08 19:50:14
 * @Description 货柜业务接口实现
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-08 19:50:14		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@Service
public class CabinetServiceImpl implements ICabinetService {
    private static final Logger logs = LoggerFactory.getLogger(CabinetServiceImpl.class);

    @Resource
    private ICabinetSiteDao cabinetSiteDao;

    @Resource
    private ICabinetDao cabinetDao;

    @Resource
    private ICabinetCellDao cabinetCellDao;

    @Resource
    private IContainerScanDao containerScanDao;

    @Override
    public ContainerScanDO getScanMsg(String scanId){
        return containerScanDao.getById(scanId);
    }

    @Override
    public boolean saveOrUpdateScanMsg(ContainerScanDO domain){
        return containerScanDao.saveOrUpdate(domain);
    }

    @Override
    public List<CabinetBO> searchList(CabinetListQueryBO bo) {
        List<ContainerDO> list = cabinetDao.search(bo);
        return CabinetConvert.domainTOBOList(list);
    }

    @Override
    public CabinetBO select(Long cabinetId) {
        ContainerDO cabinet = cabinetDao.getById(cabinetId);
        return CabinetConvert.domainTOBO(cabinet);
    }

    @Override
    public ContainerDO getById(Long cabinetId) {
        return  cabinetDao.getById(cabinetId);
    }

    @Override
    public CabinetBO select(CabinetSelectQueryBo bo) {
        ContainerDO cabinet = cabinetDao.select(bo);
        return CabinetConvert.domainTOBO(cabinet);
    }

    /**
     * @return boolean 新增结果
     * @Title create
     * @Desc 新增货柜
     * @Date 2024-04-08 16:52:16
     * @Param cabinet 货柜信息
     */
    @Override
    public boolean create(ContainerDO cabinet) {
        return cabinetDao.save(cabinet);
    }

    /**
     * @return boolean 修改结果
     * @Title update
     * @Desc 修改货柜
     * @Date 2024-04-08 16:55:09
     * @Param cabinet 货柜信息
     */
    @Override
    public boolean update(ContainerDO cabinet) {
        return cabinetDao.updateById(cabinet);
    }

    /**
     * @param
     * @Title changeAgent
     * @Desc 更换代理
     * @Date 2024-09-13 19:32:01.212
     * @Return boolean 修改结果
     */
    @Override
    public boolean changeAgent(ChangeBindBO bo) {
        return cabinetDao.changeAgent(bo);
    }

    /**
     * @param ids 货柜ID数组
     * @Title delete
     * @Desc 删除货柜信息
     * @Date 2024-06-07 16:38:45.317
     * @Return boolean 删除结果
     */
    @Override
    public boolean delete(Long... ids) {
        return cabinetDao.removeByIds(Arrays.asList(ids));
    }

    /**
     * @param
     * @Title checkCabinet
     * @Desc 检验货柜是否占用
     * @Date 2024-06-07 16:35:37.390
     * @Return boolean true 不占用 false 占用
     */
    @Override
    public boolean checkCabinet(CabinetCheckBO bo) {
        return cabinetDao.existsCabinet(bo);
    }

    /**
     * @Title resetCabinet
     * @Desc 重置货柜二维码信息
     * @Date 2024-06-07 16:21:27.659
     * @Return boolean 重置结果
     */
    @Override
    public boolean resetCabinetQrcode(Long cabinetId) {
        return cabinetDao.resetCabinetQrcode(cabinetId);
    }

    /**
     * @param
     * @Title agentCabinetCount
     * @Desc 代理货柜统计
     * @Date 2024-09-02 11:59:01.290
     * @Return int 统计结果
     */
    @Override
    public int countCabinet(CabinetCountBO bo) {
        return cabinetDao.countCabinet(bo);
    }

    /**
     * @param param 过滤条件
     * @Title searchTableField
     * @Desc 匹配表字段信息
     * @Date 2024-09-09 11:40:46.462
     * @Return List<String> 匹配结果
     */
    @Override
    public List<String> searchTableField(StorageFiledDTO param) {
        return cabinetDao.searchTableField(param);
    }


    @Override
    public List<Long> searchProductIdsBySiteId(Long siteId) {
        List<Long> containerIds = cabinetDao.searchContainerIdsBySiteId(siteId);
        if (BeanUtils.isNull(containerIds)) return new ArrayList<>();
        List<String> productIds = cabinetCellDao.searchProductIdsByContainerId(containerIds);
        return productIds.stream().distinct().mapToLong(t -> Long.valueOf(t)).boxed().collect(Collectors.toList());
    }

    @Override
    public SiteDO getSiteByCid(Long containerId) {
        CabinetSiteDO cs = cabinetSiteDao.getCabinetSiteByCid(containerId);
        return BaseCache.getSite(cs.getSiteId());
    }

    @Override
    public Long getBelongIdByContainerId(Long containerId) {
        return cabinetDao.getBelongIdByContainerId(containerId);
    }

    @Override
    public ContainerDO getByQrcode(String qrCode) {
        return cabinetDao.getByQrcode(qrCode);
    }

    @Override
    public List<SpecificUpgradeCabinetVO> specificUpgradeCabinetVOList(SpecificUpgradeCabinetParam param) {
        List<ContainerDO> list = cabinetDao.specificUpgradeCabinetVOList(param);
        return CabinetConvert.domainTOSpecificList(list);
    }

    @Override
    public List<ContainerDO> specificUpgradeCabinetList(SpecificUpgradeCabinetBO bo) {
        return cabinetDao.specificUpgradeCabinetList(bo);
    }

    @Override
    public boolean containerExistsSiteLocation(List<Long> locationIds) {
        return cabinetDao.containerExistsSiteLocation(locationIds);
    }

    @Override
    public boolean exist(CabinetSelectQueryBo build) {
        return cabinetDao.existsCabinet(build);
    }


    @Override
    public CabinetCacheDTO getCacheByImei(String imei) {
        CabinetCacheDTO cache = new CabinetCacheDTO();
        ContainerDO domain = cabinetDao.getByImei(imei);
        BeanUtils.clone(domain, cache);
        return cache;
    }

    @Override
    public String getImeiByQrcode(String qrcode) {
        return cabinetDao.getImeiByQrcode(qrcode);
    }

    @Override
    public String getImeiById(Long id) {
        return cabinetDao.getImeiById(id);
    }


    @Override
    public List<ContainerDO> search(CabinetListQueryBO bo) {
        return cabinetDao.search(bo);
    }
}
